package com.newcoder.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Arrays_interset {

    public static void main(String[] args) {
        int[] A = new int[]{1,2,2,1};
        int[] B = new int[]{2,2};
        int[] result = new Arrays_interset().intersect(A, B);
        System.out.println(Arrays.toString(result));
    }

    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> tempList = new ArrayList<>();
        int index = 0;
        if (nums1.length<=nums2.length) {
            for (int i = 0; i < nums1.length; i++) {
                for (int j = 0; j < nums2.length; j++) {
                    if (nums1[i]==nums2[j]){
                        tempList.add(nums1[i]);
                        break;
                    }
                }
            }
        } else {
            for (int i = 0; i < nums2.length; i++) {
                for (int j = 0; j < nums1.length; j++) {
                    if (nums2[i]==nums1[j]){
                        tempList.add(nums2[i]);
                        break;
                    }
                }
            }
        }
        // 把集合list转化为数组
        int[] res = new int[tempList.size()];
        for (int i = 0; i < tempList.size(); i++) {
            res[i] = tempList.get(i);
        }

        return res;
    }
}
